1 : <?php
2 : /*--------------------------------------------------------------------------+
3 : This file is part of eStudy.
4 : - Modulgruppe: News
5 : - Beschreibung: Zugriff auf die Tabelle der Kategorien
6 : - Version: 1.0, 28/03/09
7 : - Autor(en): Christoph Thelen <christoph.thelen@mni.fh-giessen.de>
8 : +---------------------------------------------------------------------------+
9 : This program is free software; you can redistribute it and/or
10 : modify it under the terms of the GNU General Public License
11 : as published by the Free Software Foundation; either version 2
12 : of the License, or any later version.
13 : +---------------------------------------------------------------------------+
14 : This program is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU General Public License for more details.
18 : You should have received a copy of the GNU General Public License
19 : along with this program; if not, write to the Free Software
20 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 : +--------------------------------------------------------------------------*/
22 :
23 : if (! defined("PATH_TO_ROOT")) define("PATH_TO_ROOT", "../../");
24 :
25 : require_once "class.category.inc.php";
26 : require_once "class.categories.inc.php";
27 :
28 : class CategoryGateway {
29 :
30 : /**
31 : * @var ezdb
32 : */
33 : private $db;
34 :
35 : /**
36 : * @var Data
37 : */
38 : private $data;
39 :
40 : private $userid;
41 :
42 : public function __construct(ezdb $db, $data, $userid) {
43 14 : $this->db = $db;
44 14 : $this->data = $data;
45 14 : $this->userid = (int)$userid;
46 14 : }
47 :
48 : public function store(array $order) {
49 8 : $sql = "INSERT INTO news_categories (`UserID`, `CategoryID`, `Position`, `Color`, `Visible`) VALUES ";
50 8 : foreach ($order as $category) {
51 8 : $sql .= $this->asSqlValues($category->getId(), $category->getPosition(), $category->getColor(), $category->isVisible());
52 8 : }
53 8 : $sql = $this->deleteDelimeter($sql);
54 8 : $sql .= " ON DUPLICATE KEY UPDATE `Position` = VALUES(`Position`), `Color` = VALUES(`Color`), `Visible` = VALUES(`Visible`)";
55 8 : $this->db->query($sql);
56 8 : }
57 :
58 : private function asSqlValues($categoryid, $position, $color, $visibility) {
59 8 : $catid = (int)$categoryid;
60 8 : $position = (int)$position;
61 8 : $color = (strlen($color)) ? "'" . $this->data->toMysql($color) . "'" : 'NULL';
62 8 : $visibility = $visibility ? 1 : 0;
63 8 : return "('$this->userid', '$catid', '$position', $color, '$visibility'),";
64 : }
65 :
66 : private function deleteDelimeter($sql) {
67 8 : return substr($sql, 0, -1);
68 : }
69 :
70 : public function load() {
71 6 : $sql = "SELECT CategoryID AS id, Color AS color, Visible AS visible FROM news_categories WHERE UserID = '$this->userid' ORDER BY Position";
72 6 : $resultSet = (array)$this->db->get_results($sql, ARRAY_A);
73 6 : return Category::create($resultSet);
74 : }
75 :
76 : public function loadVisible() {
77 3 : $sql = "SELECT CategoryID AS id, Color AS color, Visible AS visible FROM news_categories WHERE UserID = '$this->userid' AND Visible = 1 ORDER BY Position";
78 3 : $resultSet = (array)$this->db->get_results($sql, ARRAY_A);
79 3 : if ($this->db->num_rows == 0 && $this->userHasNoSettings()) {
80 1 : $resultSet = $this->createDefaultResultSet();
81 1 : }
82 3 : return new Categories(Category::create($resultSet));
83 : }
84 :
85 : private function userHasNoSettings() {
86 2 : return $this->db->get_var("SELECT COUNT(*) FROM news_categories WHERE UserID = '$this->userid'") == 0;
87 : }
88 :
89 : private function createDefaultResultSet() {
90 1 : require_once 'class.news.inc.php';
91 :
92 1 : $defaultOrder = News::getCategoryNames();
93 1 : $resultSet = array();
94 1 : foreach ($defaultOrder as $id => $name) {
95 1 : $resultSet[] = array('id' => $id, 'color' => '', 'visible' => 1);
96 1 : }
97 1 : return $resultSet;
98 : }
99 :
100 : public function reset() {
101 2 : $sql = "DELETE FROM news_categories WHERE UserID = '$this->userid'";
102 2 : $this->db->query($sql);
103 2 : }
|